package com.xs.ahoi.hcxs.service;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import com.xs.ahoi.hcxs.Ahoi;
import com.xs.ahoi.hcxs.ui.activity.BookContentActivity;
import com.xs.ahoi.hcxs.ui.view.SmallWindowView;
import com.xs.ahoi.hcxs.utils.Constants;
import com.xs.ahoi.hcxs.utils.LogUtil;

public class FloatWinsService extends Service {

    private WindowManager windowManager;
    private SmallWindowView view;
    private int mLastY, mLastX;
    private WindowManager.LayoutParams layoutParams;
    private int shortestMoveSlop;

    public FloatWinsService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        showFloatWins();
        shortestMoveSlop = ViewConfiguration.get(Ahoi.getInstance().getApplicationContext()).getScaledTouchSlop();
        return super.onStartCommand(intent, flags, startId);
    }

    private void showFloatWins() {
        if (view != null) return;
        if (windowManager == null) {
            windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

            view = new SmallWindowView(this);
            view.setOnTouchListener(touchListener);

            // 设置LayoutParam
            layoutParams = new WindowManager.LayoutParams();
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            layoutParams.format = PixelFormat.TRANSPARENT;
            layoutParams.width = 200;
            layoutParams.height = 200;
            layoutParams.x = 100;
            layoutParams.y = 300;
            LogUtil.e(layoutParams.x + "-" + layoutParams.y);

            // 将悬浮窗控件添加到WindowManager
            windowManager.addView(view, layoutParams);
        }
    }


    private boolean isMoved = false;
    private int mTouchDownX, mTouchDownY;
    private int mTouchX, mTouchY;
    View.OnTouchListener touchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mTouchX = (int) event.getRawX();
            mTouchY = (int) event.getRawY();
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    isMoved = false;
                    mLastX = (int) event.getRawX();
                    mLastY = (int) event.getRawY();
                    mTouchDownX = mLastX;
                    mTouchDownY = mLastY;
                    break;
                case MotionEvent.ACTION_MOVE:
                    // 获取有效移动的最小距离，判断是否移动
                    if (!isMoved) {
                        isMoved = validMoveSlop();
                    }
                    layoutParams.x += mTouchX - mLastX;
                    layoutParams.y += mTouchY - mLastY;
                    mLastX = mTouchX;
                    mLastY = mTouchY;
                    updateWindowLayout();
                    break;
                case MotionEvent.ACTION_UP:
                    if (!isMoved) {
                        openContentView();
                    }
                    break;
            }
            return false;
        }
    };

    public void openContentView() {
        try {

            Intent intent = new Intent(Ahoi.getInstance().getApplicationContext(), BookContentActivity.class);
            intent.putExtra(Constants.DEFAULT_ID, BookFactory.getInstance().getBookId());
            startActivity(intent);
        }catch (Exception e){
            LogUtil.e("error:"+e.getMessage());
        }
    }

    // 移动距离是否有效
    private boolean validMoveSlop() {
        return Math.abs(mTouchDownX - mTouchX) > shortestMoveSlop || Math.abs(mTouchDownY - mTouchY) > shortestMoveSlop;
    }

    public void updateWindowLayout() {
        if (windowManager != null && layoutParams != null) {
            windowManager.updateViewLayout(view, layoutParams);
        }
    }

    public void destroyWindow() {
        if (windowManager != null && view != null) {
            windowManager.removeView(view);
            view = null;
        }
    }

    @Override
    public void onDestroy() {
        destroyWindow();
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
